﻿using System.Linq;
using Sfd.Repositories;
using Sfd.Repositories.SqlClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Sfd.Entities;

namespace SqlIntegrationTests
{   
    /// <summary>
    ///This is a test class for ProjectRepositoryTest and is intended
    ///to contain all ProjectRepositoryTest Unit Tests
    ///</summary>
    [TestClass]
    public class When_I_test_project_repository
    {
        private IProjectRepository _repository;

        [TestInitialize]
        public void Init()
        {
            _repository = new ProjectRepository();
        }

        /// <summary>
        ///A test for IsPublished
        ///</summary>
        [TestMethod]
        public void A_published_project_is_present_in_db()
        {
            var project = new AzureSyncProject
            {
                AccountName = "accountName",
                ContainerName = "containerName",
                FriendlyName = "friendlyName",
                Id = Guid.NewGuid(),
                SharedKey = "sharedKey"
            };
            _repository.PublishProject(project);

            var result = _repository.IsPublished(project.Id);
            Assert.AreEqual(1, result);

            _repository.Unpublish(project.Id);
        }

        /// <summary>
        ///A test for IsPublished
        ///</summary>
        [TestMethod()]
        public void A_unpublished_project_is_not_present_in_db()
        {
            var result = _repository.IsPublished(Guid.NewGuid());
            Assert.AreEqual(0, result);
        }

        /// <summary>
        ///A test for PublishProject
        ///</summary>
        [TestMethod]
        public void A_valid_project_is_published()
        {
            var project = new AzureSyncProject
                                           {
                                               AccountName = "accountName",
                                               ContainerName = "containerName",
                                               FriendlyName = "friendlyName",
                                               Id = Guid.NewGuid(),
                                               SharedKey = "sharedKey"
                                           };
            var result = _repository.PublishProject(project);

            Assert.AreEqual(1, result);

            _repository.Unpublish(project.Id);
        }

        /// <summary>
        ///A test for Unpublish
        ///</summary>
        [TestMethod]
        public void UnpublishTest()
        {
            var project = new AzureSyncProject
            {
                AccountName = "accountName",
                ContainerName = "containerName",
                FriendlyName = "friendlyName",
                Id = Guid.NewGuid(),
                SharedKey = "sharedKey"
            };
            _repository.PublishProject(project);

            var result = _repository.Unpublish(project.Id);

            Assert.AreEqual(1, result);
        }

        [TestMethod]
        public void I_can_add_a_user_to_a_project()
        {  
            var project = new AzureSyncProject
            {
                AccountName = "accountName",
                ContainerName = "containerName",
                FriendlyName = "friendlyName",
                Id = Guid.NewGuid(),
                SharedKey = "sharedKey"
            };
            _repository.PublishProject(project);

            var userId = new Guid("D97793CB-C94E-4064-BBCB-9B7D49819C5A");

            var result = _repository.AddUserInProject(userId, project.Id);

            Assert.AreEqual(1, result);

            _repository.RemoveUserFromProject(userId, project.Id);

            _repository.Unpublish(project.Id);
         }

        [TestMethod]
        public void I_can_remove_a_user_from_a_project()
        {
            var project = new AzureSyncProject
                              {
                                  AccountName = "accountName",
                                  ContainerName = "containerName",
                                  FriendlyName = "friendlyName",
                                  Id = Guid.NewGuid(),
                                  SharedKey = "sharedKey"
                              };
            _repository.PublishProject(project);

            var userId = new Guid("D97793CB-C94E-4064-BBCB-9B7D49819C5A");

            _repository.AddUserInProject(userId, project.Id);

            var result = _repository.RemoveUserFromProject(userId, project.Id);
            Assert.AreEqual(1, result);

            _repository.Unpublish(project.Id);
        }

        [TestMethod]
        public void I_can_test_a_user_in_a_project()
        {
            var project = new AzureSyncProject
            {
                AccountName = "accountName",
                ContainerName = "containerName",
                FriendlyName = "friendlyName",
                Id = Guid.NewGuid(),
                SharedKey = "sharedKey"
            };
            _repository.PublishProject(project);

            var userId = new Guid("D97793CB-C94E-4064-BBCB-9B7D49819C5A");

            _repository.AddUserInProject(userId, project.Id);

            var result = _repository.IsUserInProject(userId, project.Id);

            Assert.AreEqual(1, result);

            _repository.RemoveUserFromProject(userId, project.Id);

            _repository.Unpublish(project.Id);
        }

        [TestMethod]
        public void I_retreive_project_from_user()
        {
            var project = new AzureSyncProject
            {
                AccountName = "accountName",
                ContainerName = "containerName",
                FriendlyName = "friendlyName",
                Id = Guid.NewGuid(),
                SharedKey = "sharedKey"
            };
            _repository.PublishProject(project);

            var userId = new Guid("D97793CB-C94E-4064-BBCB-9B7D49819C5A");

            _repository.AddUserInProject(userId, project.Id);

            var result = _repository.GetUserProjects(userId);

            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(project.Id, result.First().Id);
            Assert.AreEqual(project.AccountName, result.First().AccountName);
            Assert.AreEqual(project.ContainerName, result.First().ContainerName);
            Assert.AreEqual(project.FriendlyName, result.First().FriendlyName);
            Assert.AreEqual(project.SharedKey, result.First().SharedKey);

            _repository.RemoveUserFromProject(userId, project.Id);

            _repository.Unpublish(project.Id);
        }
    }
}
